#import "levelSelectScene.h"
#include "game_logic.h"
#include <boost/scoped_array.hpp>
#include <boost/lexical_cast.hpp>   
#include <string>
using namespace cocos2d;
#include "smart_ptr4ObjC.h"
#include "serialization_wrap.h"

namespace {
	const menu_item_info_t G_ITEMS[] ={ {{74, 16}, "level_select_back_normal.png", "level_select_back_select.png", "level_select_back_normal.png"},
		{{290, 16}, "level_select_prev_normal.png", "level_select_prev_select.png", "level_select_prev_normal.png"},
		{{406, 16}, "level_select_next_normal.png", "level_select_next_select.png", "level_select_next_normal.png"},
	};
	
	const image_info_t G_IMAGE_BG[] = {
		{{SCREEN_WIDTH/2, SCREEN_HEIGHT/2}, "background.png"},
		{{SCREEN_WIDTH/2, 300}, "level_select_title.png"},
	};
	const image_info_t G_IMAGE_TEXT = {{SCREEN_WIDTH/2, SCREEN_HEIGHT/2}, "level_select_text.png"};
	const image_info_t G_IMAGE_DISABLE = {{SCREEN_WIDTH/2, SCREEN_HEIGHT/2}, "level_disable.png"};	
	const image_info_t G_IMAGE_SCORE = {{SCREEN_WIDTH/2, SCREEN_HEIGHT/2}, "level_select_score.png"};
};

const CGPoint kMENU_START_POS = {77, 232};
const CGPoint kMENU_TEXT_OFFSET = {0, -6.0};
const CGPoint kMENU_DISABLE_OFFSET = {0, 0.0};
const CGPoint kMENU_SCORE_OFFSET = {30, 26.0};
const int kMENU_ITEM_PER_ROW = 5;
const int kMENU_ITEM_ROW_NUM = 3;
const CGSize kMENU_DISTANCE = {82, 75};
const CGPoint kMENU_LEVEL_POS_OFFSET = {6, -36};

void LevelSelectScene::menuCallback2game(CCObject* sender)
{
	//UXLOG("selected item: %x index:%d", dynamic_cast<CCMenuItemToggle*>(sender)->selectedItem(), dynamic_cast<CCMenuItemToggle*>(sender)->selectedIndex() ); 

	unsigned int selected = dynamic_cast<CCMenuItemImage*>(sender)->getTag();
//	game_logic::setCurLevel([m_menu.get() selectedItem] + m_startIndex);
//	scoped_object_c_ptr<gameScene> game([[gameScene alloc] init]);
//	[[Director sharedDirector] replaceScene:[SlideInRTransition transitionWithDuration:1.2f scene:game]];
	
}

void LevelSelectScene::menuCallback(CCObject* sender)
{
	
}

void LevelSelectScene::init(int  start_index, int max_available)
{	
	CCScene::init();
	int kTOTAL_LEVELS = game_logic::totalLevel();
	//todo, remove this
	kTOTAL_LEVELS = 30;
	start_index = (start_index / (kMENU_ITEM_PER_ROW*kMENU_ITEM_ROW_NUM)) * kMENU_ITEM_PER_ROW*kMENU_ITEM_ROW_NUM;
	int end_index = (kTOTAL_LEVELS > (start_index + kMENU_ITEM_PER_ROW*kMENU_ITEM_ROW_NUM)) ? 
			(start_index + kMENU_ITEM_PER_ROW*kMENU_ITEM_ROW_NUM) : kTOTAL_LEVELS;

	typedef CCMenuItem* pMenuItem;
	boost::scoped_array<pMenuItem> menu_items(new pMenuItem[end_index - start_index + 1]);
	for (int i = 0; i < end_index - start_index; i++) {
		menu_items[i] = CCMenuItemImage::itemFromNormalImage(kImages[i+start_index][0], kImages[i+start_index][1], kImages[i+start_index][2],
															 this, menu_selector(LevelSelectScene::menuCallback2game));
		CGPoint pos = kMENU_START_POS;
		pos.y -= kMENU_DISTANCE.height * (i/kMENU_ITEM_PER_ROW);
		pos.x += kMENU_DISTANCE.width * (i%kMENU_ITEM_PER_ROW);
		::setPosition(menu_items[i], pos);
		menu_items[i]->setTag(start_index+i);
		if ((start_index + i) > max_available) {
			menu_items[i]->setIsEnabled(false);
			scoped_object_c_ptr<CCSprite> tmp;		
			setImage(tmp, G_IMAGE_DISABLE);
			::setPosition(tmp.get(), pos+kMENU_DISABLE_OFFSET);
			addChild(tmp.get(), 3);
		}
		else {
			if (0 != game_logic::score(start_index + i)) {				
				scoped_object_c_ptr<CCSprite> tmp;
				setImage(tmp, G_IMAGE_SCORE);
				::setPosition(tmp.get(), pos+kMENU_SCORE_OFFSET);
				addChild(tmp.get(), 3);
			}
		}
		
		scoped_object_c_ptr<CCSprite> tmp;	
		setImage(tmp, G_IMAGE_TEXT);
		::setPosition(tmp.get(), pos+kMENU_TEXT_OFFSET);
		addChild(tmp.get(), 1);
		
		std::string str = boost::lexical_cast<std::string>(start_index+i+1);				
		CCLabelAtlas *level_lable = CCLabelAtlas::labelWithString(str.c_str(), "level_numbers.png", 8, 9, '0');		
		::setPosition(level_lable, pos + kMENU_LEVEL_POS_OFFSET);
		addChild(level_lable, 3);
	}
	menu_items[end_index - start_index] = NULL;
	m_menu.reset(new CCMenu());
	m_menu->initWithItems(menu_items[0], (va_list)(&menu_items[1]));	
	m_menu->setOpacity(255);
	::setPosition(m_menu.get(), CCPoint(0.0f, 0.0f));
	m_startIndex = start_index;
	m_endIndex = end_index;
	addChild(m_menu.get(), 2);
	
	pMenuItem items[dim(G_ITEMS) + 1] = {0};
	for (int i = 0; i < dim(G_ITEMS); i++) {
		items[i] = getMenuItem(G_ITEMS[i], this, menu_selector(LevelSelectScene::menuCallback));
	}
	if (0 == m_startIndex) items[1]->setIsEnabled(false);
	if (kTOTAL_LEVELS == m_endIndex) items[2]->setIsEnabled(false);
	
	m_menu4ui.reset(new CCMenu());
	m_menu4ui->initWithItems(items[0], (va_list)(&items[1]));
	m_menu4ui->setOpacity(255);
	::setPosition(m_menu4ui.get(), CCPoint(0.0f, 0.0f));
	addChild(m_menu4ui.get());
	
	for(int i = 0; i < dim(G_IMAGE_BG); i++) {
		scoped_object_c_ptr<CCSprite> tmp;
		setImage(tmp, G_IMAGE_BG[i]);
		addChild(tmp.get(), -1);
	}
}
